package com.zhuge_new;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.Dialog.Home_Dialog;
import com.zhuge_new.Fragment.HomeFragment;
import com.zhuge_new.Fragment.MineFragment;
import com.zhuge_new.Fragment.PachedFragment;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private List<Fragment> list=new ArrayList<>();
    private HomeFragment homeFragment;
    private PachedFragment pachedFragment;
    private MineFragment mineFragment;
    private LinearLayout frame_home;
    private LinearLayout main_check1;
    private LinearLayout main_check2;
    private LinearLayout main_check3;
    private Fragment currentfragment;
    private FragmentManager fragmentManager;
    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setStatusBarLightMode(MainActivity.this);
        StatusBarUtil.setStatusBarColor(MainActivity.this,R.color.white);
        PreferencesUtils.putString(this,"token","D27D5FAE17D940F4BB0E08DFB614B6B9");
        PreferencesUtils.putString(this,"cell_phone","18437312484");
        init_title();
        initFragment();

    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        pachedFragment = new PachedFragment();
        mineFragment = new MineFragment();
        fragmentManager = getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction transaction= fragmentManager.beginTransaction();
        //默认首页
        transaction.add(R.id.frame_content,homeFragment).commit();
        currentfragment = homeFragment;
        //设置选中状态
        frame_home.setSelected(true);
    }


    public void init_title() {
        main_check1 = findViewById(R.id.main_check1);
        main_check2 = findViewById(R.id.main_check2);
        main_check3 = findViewById(R.id.main_check3);
        frame_home = findViewById(R.id.frame_home);
        main_check1.setOnClickListener(this);
        main_check2.setOnClickListener(this);
        main_check3.setOnClickListener(this);
        Home_Dialog home_dialog=new Home_Dialog(this);
        home_dialog.onCreateView();
        home_dialog.setUiBeforShow();
        home_dialog.show();
    }

    @Override
    public void onBackPressed() {
        long secondTime=System.currentTimeMillis();
        if (secondTime - firstTime>3000){
            MToast.makeTextShort(MainActivity.this,"再按一次退出程序").show();
            firstTime=secondTime;
        }else{
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_check1:
                reset();
                main_check1.setSelected(true);
                SwitchFragment(homeFragment);
                break;
            case R.id.main_check2:
                reset();
                main_check2.setSelected(true);
                SwitchFragment(pachedFragment);
                break;
            case R.id.main_check3:
                reset();
                main_check3.setSelected(true);
                SwitchFragment(mineFragment);
                break;

        }

    }
    //重置选中状态
    private void reset() {
        main_check1.setSelected(false);
        main_check2.setSelected(false);
        main_check3.setSelected(false);
    }
    //切换fragment
    private void SwitchFragment(Fragment tofragment) {

        if (currentfragment != tofragment) {

            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (!tofragment.isAdded()) {

                transaction.hide(currentfragment).add(R.id.frame_content, tofragment).commit();

            } else {

                transaction.hide(currentfragment).show(tofragment).commit();
            }

            currentfragment = tofragment;
        }

    }
}
